package com.company.编译原理.DFA;

import java.io.*;
import java.util.HashMap;
import java.util.regex.Pattern;

/**
 * @ClassName DFA
 * @Description TODO
 * @Author 11
 * @Date 2021/3/10 16:44
 * @Version 1.0
 */
public class DFA {

    //定义字符、状态
    StringBuffer inputString;//要匹配的字符集
    String letterPattern="[A-Za-z][A-Za-z]*";//字母集
//    String commentPattern="(/\\*(.|\\r\\n)*\\*/ | //[\\s\\S]*?\\n)";//数字集
//    String operatorPattern;//操作符集
//    String assignmentPattern;//赋值符集
    int count= 0;//计数
    int currState;//开始状态编号 默认1
    final int  acceptState=1; //标识符接受状态
    final int  startState=0;//开始状态
    final int  comment1State=2;//    /   注释状态1
    final int  comment2State=3;//    *   注释状态2
    final int  comment3State=4;//    *   注释状态3
    final int  comment4State=5;//    /   注释接受状态



    public DFA(StringBuffer inputString) {
        this.inputString = inputString;
    }

    public void runDFA(){
//        new HashMap<>()
        currState = startState;
        StringBuffer res = new StringBuffer();//标识符集
        StringBuffer commentString = new StringBuffer(); //注释集
//        System.out.println(inputString);
        for(int i=0;i < inputString.length();i++) {

            char currChar = inputString.charAt(i);;  //当前字符
//            System.out.println(currChar);
            if (currState==startState){
                boolean isletter = Pattern.matches(letterPattern, String.valueOf(currChar));//是否字母
                boolean iscomment =Pattern.matches("/",String.valueOf(currChar));    //是否 /开头
//                System.out.println(currChar+"  "+isletter+" "+iscomment);
                if (isletter){
                    res.append(currChar);
                    currState=acceptState;
                    continue;
                }
                if (iscomment){
                    commentString.append(currChar);
                    currState=comment1State;
                }




            }else if(currState==comment1State){
                boolean iscomment = Pattern.matches("\\*",String.valueOf(currChar));
                System.out.println(currChar+"  "+" "+iscomment);
                if (!iscomment){//没有匹配到/*。回到开始
                    commentString=new StringBuffer();//重置注释数组
                    currState=startState;
                    continue;
                }
                inputString.delete(i-1,i+1);//删除字符   /*
                i-=2;

                commentString.append(currChar);
                //否则改变状态
                currState=comment2State;
            }else if (currState==comment2State){
                inputString.delete(i,i+1);//删除注释内容字符
                i--;
                commentString.append(currChar);
                boolean iscomment = Pattern.matches("\\*",String.valueOf(currChar));
                if (iscomment){//匹配到/* *了改变状态
                    currState=comment3State;
                    continue;
                }
                //匹配不到状态不变
                currState=comment2State;
            }else if (currState==comment3State){
                inputString.delete(i,i+1);//删除注释内容字符
                i--;
//                i++;
                commentString.append(currChar);
                boolean iscomment =Pattern.matches("/",String.valueOf(currChar));
                if (iscomment){//匹配到/* */改变状态
                    currState=comment4State;
                    continue;
                }
                //匹配不到状态不变
                currState=comment2State;
            }else if (currState==comment4State){
                //注释的最终接受状态
                //输出注释
                //返回开始状态
//                inputString.delete(i,i+1);//删除注释内容字符
//                i--;
////                i++;
//                commentString.append(currChar);
                System.out.println(inputString.toString());
                System.out.println("注释：  "+commentString);
                currState=startState;
            } else if (currState==acceptState){

                boolean isIDMatch = Pattern.matches(letterPattern, String.valueOf(currChar));
                //如果匹配到字母
                if (isIDMatch){
                    res.append(currChar);
                    continue;
                }
                //如果匹配不到字母 标识符++  然后去匹配是否注释

                count++;
                res.append(" ");
                boolean iscomment =Pattern.matches("/",String.valueOf(currChar));    //是否 /开头
//                System.out.println(currChar+"  "+iscomment);
                if (iscomment){
                    commentString.append(currChar);
                    currState=comment1State;
                    continue;
                }
                currState=startState;

//                if (!isIDMatch){ //匹配不到字母  再去匹配是否注释开始 /    标识符++

//                count++;
//                res.delete(res.length(),res.length()+1);
//                System.out.println(res.substring(0,res.length())+"   "+count);
//                res= new StringBuffer(res.substring(0,res.length()) +" ");
//                currState=startState;

//                }

            }else {
                System.out.println("error!");
                return;
            }

        }
        System.out.println(res);
        System.out.println(commentString);
        System.out.println("ok "+count);
    }



    public static void main(String[] args) {

        Object o =new Object();


        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader  reader = null;
        try {
            reader= new BufferedReader(new FileReader("F:\\workProject\\interview-questions\\javacode\\src\\main\\java\\com\\company\\编译原理\\DFA\\test.txt"));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                stringBuffer.append(tempStr);
            }
            reader.close();

            DFA test = new DFA(stringBuffer);
            test.runDFA();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

    }
}
